Ext.ns('AmCheckView');

AmCheckView = Ext.extend(Ext.Panel, {
	// 条件搜索Panel
	searchPanel : null,
	// 数据展示Panel
	gridPanel : null,
	// GridPanel的数据Store
	store : null,
	// 头部工具栏
	topbar : null,
	// 构造函数
	constructor : function(_cfg) {
		Ext.applyIf(this, _cfg);
		// 初始化组件
		this.initUIComponents();
		// 调用父类构造
		AmCheckView.superclass.constructor.call(this, {
					id : 'AmCheckView',
					title : '电表记录審核',
					iconCls : 'menu-myplan',
					region : 'center',
					layout : 'border',
					items : [this.searchPanel, this.gridPanel]
				});
	},// end of constructor

	// 初始化组件
	initUIComponents : function() {
		var isAddGranted  = isGranted('AM_ADDPLAN');
		var isExecGranted = isGranted('AM_ADDPLAN');
		
		// 初始化搜索条件Panel
		this.searchPanel = new Ext.FormPanel({
			id : 'AmCheckSearchForm',
			height : 70,
			region : 'north',
			frame : false,
			border : false,
			padding : '10',
			layout : 'column',
			title : '查询条件',
			labelWidth : 60,
			enctype : 'multipart/form-data',
			fileUpload : true,
			url : __ctxPath + '/pdaweb/planListPdaAmMeterList.do',
			layoutConfig : {
				padding : '5',
				align : 'middle'
			},
			defaults : {
				columnWidth : 0.33,
				layout : 'form',
				border : false,
				style : 'padding-left:10px;',
				defaults : {
					xtype : 'textfield',
					width : 100
				}
			},
			items : [{
						items : [{
							        id:'am_month_show',
									fieldLabel : '抄表年月',
									xtype : 'monthfield',
									value : new Date(),
									editable : false,
									allowblank : false,
									name : 'Q_month_S_EQ'
								},{  
									id:'am_month_hide',
									xtype : 'hidden'
								}]
					}, {
						items : [{
									fieldLabel : '计划名稱',
									xtype : 'textfield',
									width : 160,
									name : 'Q_planName_S_LK'
								}]
					}, {
						items : [{
									fieldLabel : '抄表人',
									xtype : 'textfield',
									name : 'Q_executor_S_LK'
								}]
					}]

		});// end of the searchPanel

		this.store = new Ext.data.JsonStore({
					url : __ctxPath+ '/pdaweb/planListPdaAmMeterList.do',
					root : 'result',
					totalProperty : 'totalCounts',
					id : 'id',
					fields : ['id','planId', 'planName', 'executor',
						      'lastEditBy','planStatus'],
					remoteSort : true
				});
		
		
		var sm = new Ext.grid.CheckboxSelectionModel({
			handleMouseDown : Ext.emptyFn,
			listeners : {
				'beforerowselect' : function(s, i, k, r) {
					return r.get("planStatus") != '2';
				}
			},
			renderer : function(v, c, r) {
				if (r.get("planStatus") == '2') {
					return "<div style='padding:3px 0 0 3px;'><input type='checkbox' disabled /></div>";
				} else {
					return "<div class=\"x-grid3-row-checker\">&#160;</div>";
				}
			}
		});
		
		var cm = new Ext.grid.ColumnModel({
			columns : [sm, new Ext.grid.RowNumberer(), {
						header : '计划名称',
						dataIndex : 'planName',
						width : 220,
						renderer : showData
					}, {
						header : '抄表人',
						dataIndex : 'executor'
					}, {
						header : '抄表年月',
						dataIndex : 'lastEditBy'
					}],
			defaults : {
				sortable : true,
				menuDisabled : false
			}
		});// end of the cm
		
		this.topbar = new Ext.Toolbar({
					id : 'ElectricityFootBar',
					height : 30,
					bodyStyle : 'text-align:left',
					items : [new Ext.Button({
								iconCls : 'search',
								text : '查询',
								handler : function() {
									Ext.getCmp('am_month_hide').setValue(Ext.getCmp('am_month_show').getValue().format('Y-m'));
									var searchPanel = Ext.getCmp('AmCheckSearchForm');
									var gridPanel = Ext.getCmp('AmCheckGrid');
									if (searchPanel.getForm().isValid()) {
										$search({
													searchPanel : searchPanel,
													gridPanel : gridPanel
												});
										var store = gridPanel.getStore();
										var count = store.getCount();
										for (var i = 0; i < count; i++) {
											if (store.getAt(i).get("planStatus") == "1") {
												sm.selectRow(i);
											}
										}
									}
								}
							})]
				});
		
		this.gridPanel = new Ext.grid.GridPanel({
					id : 'AmCheckGrid',
					region : 'center',
					store : this.store,
					trackMouseOver : true,
					disableSelection : false,
					loadMask : true,
					autoHeight : true,
					tbar : this.topbar,
					cm : cm,
					sm : sm,
					viewConfig : {
						showPreview : false,
						getRowClass : function(record, rowIndex, rowParams,store) {// 异常状态
							var st = record.data.planStatus;
							if ('ERROR' == AmCheckView.getST(st)) {
								return 'x-grid-record-error';
							} else if ('WARN' == AmCheckView.getST(st)) {
								return 'x-grid-record-warning';
							} else {
								return '';
							}
						}
					},
					bbar : new Ext.PagingToolbar({
								pageSize : 2,
								store : this.store,
								displayInfo : true,
								displayMsg : '当前显示从{0}至{1}， 共{2}条记录',
								emptyMsg : "当前没有记录"
							})
				});
		
		if (isGranted('_AMCheck')) {
			this.topbar.add(new Ext.Button({
						iconCls : 'btn-add',
						text : '审核',
						handler : function() {
							var grid = Ext.getCmp("AmCheckGrid");
							var selectRecords = grid.getSelectionModel()
									.getSelections();

							if (selectRecords.length == 0) {
								Ext.ux.Toast.msg("信息", "请选择记录！");
								return;
							}

							var warnIds = '';
							var waitIds = '';
							for (var i = 0; i < selectRecords.length; i++) {
								var st = selectRecords[i].data.planStatus;
								if ('WAIT' == AmCheckView.getST(st)) {
									waitIds += selectRecords[i].data.planId + ',';
								} else if ('WARN' == AmCheckView.getST(st)) {
									warnIds += selectRecords[i].data.planId + ',';
								}
							}
							if (warnIds != '') {
								Ext.Msg.confirm('提示',
										'选择中包含黄颜色警示记录,你确定要审核通过吗？', function(
												btn, text) {
											if (btn == 'yes') {
												AmCheckView.check(waitIds
														+ warnIds);
											} else {
												return false;
											}
										});
							} else {
								if (waitIds != '') {
									AmCheckView.check(waitIds)
								}
							}
						}
					}));
		}
		
	}// end of the initUIComponents
});

//计划审核明细
function showData(value, cellmeta, record, rowIndex){
	return "<a href='#' onClick=AmCheckView.detail('" + value
		+ "'," + "'" + record.data["planName"] + "','"+ record.data["executor"] + "'" + ",'"
		+ record.data["lastEditBy"] + "','"+ record.data["planId"] + "')>" + value + "</a>";
	
}
//detail方法
//弹出水表审核界面
AmCheckView.detail = function(id,planName,  executor, lastEditBy,planId) {
	new AmCheckForm0({
		  	id : id,
			planName : planName,
			executor  : executor,
			lastEditBy : lastEditBy,
			planId  :  planId
		}).show();
};

AmCheckView.getST = function(st) {
	return st == '0' ? 'WAIT' : st == '2' ? 'ERROR' : st == '3' || st == '4'
			? 'WARN'
			: 'OTHER';
}

//审核
AmCheckView.check = function(ids) {
	var month = Ext.getCmp('am_month_hide').getValue();
	Ext.Ajax.request({
		url : __ctxPath
				+ '/pdaweb/checkPlanPdaAmMeterList.do?fileName='
				+ ids+"&Q_month_S_EQ="+month,
		waitMsg : '正在提交数据...',
		timeout: 30000,		
		method : 'post',
		success : function(response) {
			Ext.ux.Toast.msg("操作信息", "审核成功");
			Ext.getCmp('AmCheckGrid').getStore().reload();
		},
		failure : function() {
//			Ext.Msg.alert("操作信息", "审核失败");
			Ext.MessageBox.show({
				title : '操作信息',
				msg : '审核失败',
				buttons : Ext.MessageBox.OK,
				icon : 'ext-mb-error'
			});
		}
	});
}
